import numpy as np
from sklearn.preprocessing import StandardScaler

def normalize_features(features, scaler=None):
    """
    标准化特征
    
    参数:
        features (numpy.ndarray): 要标准化的特征
        scaler (sklearn.preprocessing.StandardScaler, optional): 
            预先拟合的标准化器。如果为None，将创建一个新的标准化器
            
    返回:
        tuple: (标准化后的特征, 使用的标准化器)
    """
    if scaler is None:
        scaler = StandardScaler()
        normalized_features = scaler.fit_transform(features)
    else:
        normalized_features = scaler.transform(features)
        
    return normalized_features, scaler

def select_features(features, feature_names, selected_features=None):
    """
    选择特定的特征
    
    参数:
        features (numpy.ndarray): 特征数组
        feature_names (list): 特征名称列表
        selected_features (list, optional): 要选择的特征名称列表
            如果为None，将返回所有特征
            
    返回:
        numpy.ndarray: 选择的特征
    """
    if selected_features is None:
        return features
    
    # 获取选择的特征的索引
    indices = [i for i, name in enumerate(feature_names) if name in selected_features]
    
    # 返回选择的特征
    return features[:, indices]

def extract_time_domain_features(features, feature_names):
    """
    提取时域特征
    
    参数:
        features (numpy.ndarray): 特征数组
        feature_names (list): 特征名称列表
        
    返回:
        numpy.ndarray: 时域特征
    """
    # 获取以't'开头的特征的索引
    indices = [i for i, name in enumerate(feature_names) if name.startswith('t')]
    
    # 返回时域特征
    return features[:, indices]

def extract_frequency_domain_features(features, feature_names):
    """
    提取频域特征
    
    参数:
        features (numpy.ndarray): 特征数组
        feature_names (list): 特征名称列表
        
    返回:
        numpy.ndarray: 频域特征
    """
    # 获取以'f'开头的特征的索引
    indices = [i for i, name in enumerate(feature_names) if name.startswith('f')]
    
    # 返回频域特征
    return features[:, indices]